/**
 * @(#)BackendAdminController.java, 2010-5-13. 
 * 
 */
package com.rsstuan.web.backend.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.rsstuan.storage.data.TuanGouChannelData;
import com.rsstuan.storage.data.TuanGouItemData;
import com.rsstuan.storage.data.TuanGouSiteData;
import com.rsstuan.storage.impl.JDOTuanGouChannelStorage;
import com.rsstuan.storage.impl.JDOTuanGouItemStorage;
import com.rsstuan.storage.impl.JDOTuanGouSiteStorage;
import com.rsstuan.utils.ItemUtils;
import com.rsstuan.web.controller.SpringController;

/**
 *
 * @author guosq
 *
 */
public class BackendAdminController extends SpringController{

	private final Logger LOG = Logger.getLogger(BackendAdminController.class.getName());
	private final static String BACKEND_FRAME = "backend/backendFrame";
	
	private final static String SITE_VM = "backend/site";
	
	private final static String CHANNEL_VM = "backend/channel";
	
	private final static String ITEM_VM = "backend/item";
	
	/*public ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("errmsg", "default");
		model.put("rightPage", SITE_VM);
		return createModelAndView(request, response, SITE_VM, model);
	}*/
	
	public ModelAndView addSite(HttpServletRequest request,
            HttpServletResponse response) {
		Map<String, Object> model = new HashMap<String, Object>();
		JDOTuanGouSiteStorage storage = new JDOTuanGouSiteStorage();
		if (request.getParameter("submit") != null) {
		    String siteId = request.getParameter("siteId");
    		String siteName = request.getParameter("siteName");
    		String link = request.getParameter("link");
    		String description = request.getParameter("description");
    		
    		TuanGouSiteData site = new TuanGouSiteData();
    		if (siteId != null) {
    		    Long id = Long.parseLong(siteId);
    		    site.setId(id);
    		}
    		site.setSiteName(siteName);
    		site.setLink(link);
    		site.setDescription(description);
    		LOG.info("get a site = " + siteName);
    		
    		
    		boolean ret = storage.saveSite(site);
    		model.put("rightPage", SITE_VM);
    		model.put("errmsg", "Add site ...");
    		if (ret) {
    			model.put("lastStatus", "ADD_SUCCESS");
    			model.put("siteName", siteName);
    			model.put("link", link);
    			model.put("description", description);
    		}
    		else {
    			model.put("lastStatus", "ADD_FAILED");
    			model.put("errmsg", "Add site failed");
    			LOG.log(Level.WARNING, "fail to add site");
    		}
		}
		
		List<TuanGouSiteData> sites = storage.getAllSites();
		model.put("sites", sites);
		return createModelAndView(request, response, SITE_VM, model);
		
	}
	
	public ModelAndView addChannel(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> model = new HashMap<String, Object>();
        JDOTuanGouChannelStorage storage = new JDOTuanGouChannelStorage();

        model.put("inputSiteId", request.getParameter("siteId"));
        model.put("inputTitle", request.getParameter("siteName"));
  
        if (request.getParameter("submit") != null) {
        	String channelId = request.getParameter("channelId");
            String title = request.getParameter("title");
            String siteId = request.getParameter("siteId");
            String city = request.getParameter("city");
            String link = request.getParameter("link");
            String rss = request.getParameter("rss");
            String description = request.getParameter("description");
            
            
            TuanGouChannelData channel = new TuanGouChannelData();
            if (channelId != null) {
    		    Long id = Long.parseLong(channelId);
    		    channel.setId(id);
    		}
            channel.setTitle(title);
            channel.setSiteId(Long.parseLong(siteId));
            channel.setCity(city);
            channel.setLink(link);
            channel.setRss(rss);
            channel.setDescription(description);
            
           
            boolean ret = storage.saveChannel(channel);
            model.put("rightPage", CHANNEL_VM);
            model.put("errmsg", "Add channel ...");
            if (ret) {
                model.put("lastStatus", "ADD_SUCCESS");
                model.put("siteId", siteId);
                model.put("city", city);
                model.put("title", title);
                model.put("link", link);
                model.put("rss", rss);
                model.put("description", description);
            }
            else {
                model.put("lastStatus", "ADD_FAILED");
                model.put("errmsg", "Add channel failed");
                LOG.log(Level.WARNING, "fail to add channel");
            }
        }
        
        Long siteId = Long.parseLong(request.getParameter("siteId"));
        List<TuanGouChannelData> channels = storage.getAllChannelsBySiteId(siteId);
        model.put("channels", channels);
        return createModelAndView(request, response, CHANNEL_VM, model);
        
    }
	
	   public ModelAndView modItem(HttpServletRequest request,
	            HttpServletResponse response) {
	        Map<String, Object> model = new HashMap<String, Object>();
	        JDOTuanGouItemStorage storage = new JDOTuanGouItemStorage();
	        List<TuanGouItemData> normals = new ArrayList<TuanGouItemData>();
	        List<TuanGouItemData> abnormals = new ArrayList<TuanGouItemData>();
	        
	        List<TuanGouItemData> all = storage.getActivateItems();
	        
	        for (TuanGouItemData item : all) {
	            if (ItemUtils.validate(item)) {
	                normals.add(item);
	            }
	            else {
	                abnormals.add(item);
	            }
	        }
	        if (request.getParameter("operation") != null
	                && request.getParameter("id") != null) {
	            String idStr = request.getParameter("id");
	            TuanGouItemData item = storage.getItem(Long.parseLong(idStr));
	            model.put("edit", true);
	            model.put("modItem", item);
	        }
	        if (request.getParameter("submit") != null) {
	            String idStr = request.getParameter("id");
	            TuanGouItemData item = storage.getItem(Long.parseLong(idStr));
	    
	            item.setTitle(request.getParameter("title"));
	            item.setImage(request.getParameter("image"));
	            item.setOrigPrice(Double.parseDouble(request.getParameter("origPrice")));
	            item.setPrice(Double.parseDouble(request.getParameter("price")));
	            item.setOrderNum(Integer.parseInt(request.getParameter("orderNum")));
	            item.setAddress(request.getParameter("address"));
	            storage.saveItem(item);
	            model.put("lastStatus", "success");
	            
	            
	        }
	        
	        model.put("count", all.size());
	        model.put("normals", normals);
	        model.put("abnormals", abnormals);
	        return createModelAndView(request, response, ITEM_VM, model);
	   }
	        
}
